Package org.jessma.hpsocket.unicode

Source Code of org.jessma.hpsocket.unicode.Server

/*
* Copyright: JessMA Open Source (ldcsaa@gmail.com)
*
* Version  : 3.2.3
* Author  : Bruce Liang
* Website  : http://www.jessma.org
* Project  : https://github.com/ldcsaa
* Blog    : http://www.cnblogs.com/ldcsaa
* Wiki    : http://www.oschina.net/p/hp-socket
* QQ Group  : 75375912
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jessma.hpsocket.unicode;

import org.jessma.hpsocket.Callback.OnAccept;
import org.jessma.hpsocket.Callback.OnClose;
import org.jessma.hpsocket.Callback.OnError;
import org.jessma.hpsocket.Callback.OnPrepareListen;
import org.jessma.hpsocket.Callback.OnPullReceive;
import org.jessma.hpsocket.Callback.OnReceive;
import org.jessma.hpsocket.Callback.OnSend;
import org.jessma.hpsocket.Callback.OnServerShutdown;
import org.jessma.hpsocket.Buffer;
import org.jessma.hpsocket.Constant;
import org.jessma.hpsocket.SocketAddress;

import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.WString;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.NativeLongByReference;
import com.sun.jna.ptr.ShortByReference;

/** Server 组件基类 (Unicode) */
public abstract class Server extends HPSocketObj
{
  /* Server 回调函数设置方法  */

  /** 设置 {@linkplain OnPrepareListen} 回调函数对象 */
  public void setCallBackOnPrepareListen(OnPrepareListen fn)
  {
    onPrepareListenImpl = fn;
    HPSocket.SDK.HP_Set_FN_Server_OnPrepareListen(socketListener, fn);
  }
 
  /** 设置 {@linkplain OnAccept} 回调函数对象 */
  public void setCallBackOnAccept(OnAccept fn)
  {
    onAcceptImpl = fn;
    HPSocket.SDK.HP_Set_FN_Server_OnAccept(socketListener, fn);
  }
 
  /** 设置 {@linkplain OnSend} 回调函数对象 */
  public void setCallBackOnSend(OnSend fn)
  {
    onSendImpl = fn;
    HPSocket.SDK.HP_Set_FN_Server_OnSend(socketListener, fn);
  }
 
  /** 设置 {@linkplain OnReceive} 回调函数对象 */
  public void setCallBackOnReceive(OnReceive fn)
  {
    onReceiveImpl = fn;
    HPSocket.SDK.HP_Set_FN_Server_OnReceive(socketListener, fn);
  }
 
  /** 设置 {@linkplain OnPullReceive} 回调函数对象 */
  public void setCallBackOnPullReceive(OnPullReceive fn)
  {
    onPullReceiveImpl = fn;
    HPSocket.SDK.HP_Set_FN_Server_OnPullReceive(socketListener, fn);
  }
 
  /** 设置 {@linkplain OnClose} 回调函数对象 */
  public void setCallBackOnClose(OnClose fn)
  {
    onCloseImpl = fn;
    HPSocket.SDK.HP_Set_FN_Server_OnClose(socketListener, fn);
  }
 
  /** 设置 {@linkplain OnError} 回调函数对象 */
  public void setCallBackOnError(OnError fn)
  {
    onErrorImpl = fn;
    HPSocket.SDK.HP_Set_FN_Server_OnError(socketListener, fn);
  }
 
  /** 设置 {@linkplain OnServerShutdown} 回调函数对象 */
  public void setCallBackOnServerShutdown(OnServerShutdown fn)
  {
    onServerShutdownImpl = fn;
    HPSocket.SDK.HP_Set_FN_Server_OnServerShutdown(socketListener, fn);
  }
 
  /* *********************************** Server 操作方法 ********************************** */
 
  /**
  * 启动通信组件
  */
  public boolean start(String pszBindAddress, short usPort)
  {
    WString pwszBindAddress = pszBindAddress != null ? new WString(pszBindAddress) : null;
    return HPSocket.SDK.HP_Server_Start(socketObj, pwszBindAddress, usPort);
  }

  /**
  * 关闭通信组件
  */
  public boolean stop()
  {
    return HPSocket.SDK.HP_Server_Stop(socketObj);
  }

  /**
  * 发送数据
、  */
  public boolean send(NativeLong dwConnID, byte[] pBuffer)
  {
    return HPSocket.SDK.HP_Server_Send(socketObj, dwConnID, pBuffer, pBuffer.length);
  }

  /**
  * 发送数据
、  */
  public boolean send(NativeLong dwConnID, Pointer pBuffer, int iLength)
  {
    return HPSocket.SDK.HP_Server_Send(socketObj, dwConnID, pBuffer, iLength);
  }

  /**
  * 发送指定起始位置的数据
  */
  public boolean sendPart(NativeLong dwConnID, byte[] pBuffer, int iLength, int iOffset)
  {
    return HPSocket.SDK.HP_Server_SendPart(socketObj, dwConnID, pBuffer, iLength, iOffset);
  }

  /**
  * 发送指定起始位置的数据
  */
  public boolean sendPart(NativeLong dwConnID, Pointer pBuffer, int iLength, int iOffset)
  {
    return HPSocket.SDK.HP_Server_SendPart(socketObj, dwConnID, pBuffer, iLength, iOffset);
  }

  /**
   * 发送多组数据
   */
  public boolean sendPackets(NativeLong dwConnID, byte[][] arrays)
  {
    return sendPackets(dwConnID, Buffer.fromByteArrays(arrays));
  }

  /**
   * 发送多组数据
   */
  public boolean sendPackets(NativeLong dwConnID, Buffer[] bufs)
  {
    int count     = bufs.length;
    long ptr     = Native.malloc(count * Buffer.NATIVE_BUFFER_SIZE);
    Pointer pBuffers = new Pointer(ptr);
   
    for(int i = 0; i < count; i++)
    {
      int offset = i * Buffer.NATIVE_BUFFER_SIZE;
      pBuffers.setInt(offset, bufs[i].getLength());
      pBuffers.setPointer(offset + Pointer.SIZE, bufs[i].getPointer());
    }
   
    boolean result = HPSocket.SDK.HP_Server_SendPackets(socketObj, dwConnID, pBuffers, count);
   
    Native.free(ptr);
    return result;
  }

  /**
  * 断开连接
  */
  public boolean disconnect(NativeLong dwConnID, boolean bForce)
  {
    return HPSocket.SDK.HP_Server_Disconnect(socketObj, dwConnID, bForce);
  }

  /**
  * 断开超时连接
  */
  public boolean disconnectLongConnections(int dwPeriod, boolean bForce)
  {
    return HPSocket.SDK.HP_Server_DisconnectLongConnections(socketObj, dwPeriod, bForce);
  }

  /* ********************************** Server 属性访问方法 ******************************** */

  /**
  * 设置连接的附加数据
  */
  public boolean setConnectionExtra(NativeLong dwConnID, NativeLong pExtra)
  {
    return HPSocket.SDK.HP_Server_SetConnectionExtra(socketObj, dwConnID, pExtra);
  }

  /**
  * 获取连接的附加数据
  */
  public boolean getConnectionExtra(NativeLong dwConnID, NativeLongByReference ppExtra)
  {
    return HPSocket.SDK.HP_Server_GetConnectionExtra(socketObj, dwConnID, ppExtra);
  }

  /** 检查通信组件是否已启动 */
  public boolean hasStarted()
  {
    return HPSocket.SDK.HP_Server_HasStarted(socketObj);
  }
 
  /** 查看通信组件当前状态 */
  public int getState()
  {
    return HPSocket.SDK.HP_Server_GetState(socketObj);
  }
 
  /** 获取最近一次失败操作的错误代码 */
  public int getLastError()
  {
    return HPSocket.SDK.HP_Server_GetLastError(socketObj);
  }
 
  /** 获取最近一次失败操作的错误描述 */
  public String getLastErrorDesc()
  {
    WString desc = HPSocket.SDK.HP_Server_GetLastErrorDesc(socketObj);
    return desc != null ? desc.toString() : null;
  }
 
  /** 获取连接中未发出数据的长度 */
  public boolean getPendingDataLength(NativeLong dwConnID, IntByReference piPending)
  {
    return HPSocket.SDK.HP_Server_GetPendingDataLength(socketObj, dwConnID, piPending);
  }
 
  /** 获取客户端连接数 */
  public int getConnectionCount()
  {
    return HPSocket.SDK.HP_Server_GetConnectionCount(socketObj);
  }
 
  /** 获取所有连接的 CONNID */
  public NativeLong[] getAllConnectionIDs()
  {
    NativeLong[] ids = null;
   
    do
    {
      int count = getConnectionCount();
     
      if(count == 0)
      {
        ids = new NativeLong[0];
        break;
      }
     
      long ptr   = Native.malloc(count * NativeLong.SIZE);
      Pointer pIDs = new Pointer(ptr);
      IntByReference refCount = new IntByReference(count);

      if(HPSocket.SDK.HP_Server_GetAllConnectionIDs(socketObj, pIDs, refCount))
      {
        count  = refCount.getValue();
        ids    = new NativeLong[count];
         
        for(int i = 0; i < count; i++)
          ids[i] = pIDs.getNativeLong(i * NativeLong.SIZE);
      }
     
      Native.free(ptr);
     
    } while (ids == null);
   
    return ids;
  }
 
  /** 获取某个客户端连接时长(毫秒) */
  public boolean getConnectPeriod(NativeLong dwConnID, IntByReference pdwPeriod)
  {
    return HPSocket.SDK.HP_Server_GetConnectPeriod(socketObj, dwConnID, pdwPeriod);
  }
 
  /** 获取监听 Socket 的地址信息 */
  public SocketAddress getListenAddress()
  {
    final int ADDR_LEN      = 40 * Constant.WCHAR_SIZE;
    byte[] lpszAddress      = new byte[ADDR_LEN];
    IntByReference piAddressLen  = new IntByReference(ADDR_LEN);
    ShortByReference pusPort  = new ShortByReference();
   
    boolean isOK = HPSocket.SDK.HP_Server_GetListenAddress(socketObj, lpszAddress, piAddressLen, pusPort);
    return SocketAddress.craete(isOK, lpszAddress, piAddressLen, pusPort, true);
  }
 
  /** 获取某个连接的远程地址信息 */
  public SocketAddress getRemoteAddress(NativeLong dwConnID)
  {
    final int ADDR_LEN      = 40 * Constant.WCHAR_SIZE;
    byte[] lpszAddress      = new byte[ADDR_LEN];
    IntByReference piAddressLen  = new IntByReference(ADDR_LEN);
    ShortByReference pusPort  = new ShortByReference();
   
    boolean isOK = HPSocket.SDK.HP_Server_GetRemoteAddress(socketObj, dwConnID, lpszAddress, piAddressLen, pusPort);
    return SocketAddress.craete(isOK, lpszAddress, piAddressLen, pusPort, true);
  }

  /** 设置数据发送策略 */
  public void setSendPolicy(int enSendPolicy)
  {
    HPSocket.SDK.HP_Server_SetSendPolicy(socketObj, enSendPolicy);
  }
 
  /** 设置数据接收策略 */
  public void setRecvPolicy(int enRecvPolicy)
  {
    HPSocket.SDK.HP_Server_SetRecvPolicy(socketObj, enRecvPolicy);
  }
 
  /** 设置 Socket 缓存对象锁定时间(毫秒,在锁定期间该 Socket 缓存对象不能被获取使用) */
  public void setFreeSocketObjLockTime(int dwFreeSocketObjLockTime)
  {
    HPSocket.SDK.HP_Server_SetFreeSocketObjLockTime(socketObj, dwFreeSocketObjLockTime);
  }
 
  /** 设置 Socket 缓存池大小(通常设置为平均并发连接数量的 1/3 - 1/2) */
  public void setFreeSocketObjPool(int dwFreeSocketObjPool)
  {
    HPSocket.SDK.HP_Server_SetFreeSocketObjPool(socketObj, dwFreeSocketObjPool);
  }
 
  /** 设置内存块缓存池大小(通常设置为 Socket 缓存池大小的 2 - 3 倍) */
  public void setFreeBufferObjPool(int dwFreeBufferObjPool)
  {
    HPSocket.SDK.HP_Server_SetFreeBufferObjPool(socketObj, dwFreeBufferObjPool);
  }
 
  /** 设置 Socket 缓存池回收阀值(通常设置为 Socket 缓存池大小的 3 倍) */
  public void setFreeSocketObjHold(int dwFreeSocketObjHold)
  {
    HPSocket.SDK.HP_Server_SetFreeSocketObjHold(socketObj, dwFreeSocketObjHold);
  }
 
  /** 设置内存块缓存池回收阀值(通常设置为内存块缓存池大小的 3 倍) */
  public void setFreeBufferObjHold(int dwFreeBufferObjHold)
  {
    HPSocket.SDK.HP_Server_SetFreeBufferObjHold(socketObj, dwFreeBufferObjHold);
  }
 
  /** 设置工作线程数量(通常设置为 2 * CPU + 2) */
  public void setWorkerThreadCount(int dwWorkerThreadCount)
  {
    HPSocket.SDK.HP_Server_SetWorkerThreadCount(socketObj, dwWorkerThreadCount);
  }
 
  /** 设置关闭服务前等待连接关闭的最长时限(毫秒,0 则不等待) */
  public void setMaxShutdownWaitTime(int dwMaxShutdownWaitTime)
  {
    HPSocket.SDK.HP_Server_SetMaxShutdownWaitTime(socketObj, dwMaxShutdownWaitTime);
  }

  /** 获取数据发送策略 */
  public int getSendPolicy()
  {
    return HPSocket.SDK.HP_Server_GetSendPolicy(socketObj);
  }

  /** 获取数据接收策略 */
  public int getRecvPolicy()
  {
    return HPSocket.SDK.HP_Server_GetRecvPolicy(socketObj);
  }

  /** 获取 Socket 缓存对象锁定时间 */
  public int getFreeSocketObjLockTime()
  {
    return HPSocket.SDK.HP_Server_GetFreeSocketObjLockTime(socketObj);
  }
 
  /** 获取 Socket 缓存池大小 */
  public int getFreeSocketObjPool()
  {
    return HPSocket.SDK.HP_Server_GetFreeSocketObjPool(socketObj);
  }
 
  /** 获取内存块缓存池大小 */
  public int getFreeBufferObjPool()
  {
    return HPSocket.SDK.HP_Server_GetFreeBufferObjPool(socketObj);
  }
 
  /** 获取 Socket 缓存池回收阀值 */
  public int getFreeSocketObjHold()
  {
    return HPSocket.SDK.HP_Server_GetFreeSocketObjHold(socketObj);
  }
 
  /** 获取内存块缓存池回收阀值 */
  public int getFreeBufferObjHold()
  {
    return HPSocket.SDK.HP_Server_GetFreeBufferObjHold(socketObj);
  }
 
  /** 获取工作线程数量 */
  public int getWorkerThreadCount()
  {
    return HPSocket.SDK.HP_Server_GetWorkerThreadCount(socketObj);
  }
 
  /** 获取关闭服务前等待连接关闭的最长时限 */
  public int getMaxShutdownWaitTime()
  {
    return HPSocket.SDK.HP_Server_GetMaxShutdownWaitTime(socketObj);
  }
 
}
TOP

Related Classes of org.jessma.hpsocket.unicode.Server

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.